<?php
@session_start();
class Payqr extends WebLoginBase{
	public $pageSize=20;
	private $vcodeSessionName='ssc_vcode_session_name';
	public $KEY = "6VfVEsa17QS5LCHUxwmMAKp7";
    public $MCHNO = "CH10";
 	public $TAG = "470";
	public $VERSION = "1.0";
	public $APPID = "1095";
	public $API = "http://pay.playgame178.com/wfNewThreepayApi";
	public $callbackUrl = 'http://lzdkjy.cn/index.php/Callback/synOrderInfo';
	public $BODYNOTE = '用户充值';

	public final function getRechId(){
		$rechargeId=mt_rand(100000,999999);
		if($this->getRow("select id from {$this->prename}member_recharge where rechargeId=$rechargeId")){
			getRechId();
		}else{
			return $rechargeId;
		}
	}
	
	public final function submitPay(){
		if(!$_POST) throw new Exception('参数出错');
		$para['mBankId']=intval($_POST['mBankId']);
		$para['amount']=floatval($_POST['amount']);
		$ret = array();

		if($para['amount']<=0) throw new Exception('充值金额错误，请重新操作');

		if($id=$para['mBankId']){
			if($id==23 || $id==24){
				if($para['amount']<1 || $para['amount']>10000) throw new Exception('支付宝扫码/微信扫码充值最低1元，最高10000元');
			}
		}else{
				throw new Exception('充值银行不存在，请重新选择');
			}

		if(strtolower($_POST['vcode'])!=$_SESSION[$this->vcodeSessionName]){
			throw new Exception('验证码不正确。');
		}else{
			// 插入充值请求表
			unset($para['coinpwd']);
			$para['rechargeId']=$this->getRechId();
			$para['actionTime']=$this->time;
			$para['uid']=$this->user['uid'];
			$para['username']=$this->user['username'];
			$para['actionIP']=$this->ip(true);
			$para['info']=$this->BODYNOTE;
			$para['bankId']=$id;
			
			$json = self::warpParam($para);
			$sign = self::sign($json);
			$json['sign']=$sign;
			$order = json_encode($json);
			//调用第三方下单
			$result = self::postCurl($order, array("content-type: application/json;charset=UTF-8"), $_SERVER['HTTP_USER_AGENT']);
			$result = json_decode($result,true);
			if(isset($result) && !empty($result) && $result['errorcode'] == 0 && ($this->MCHNO == $result['mchNo'])){
				$ret['errorcode'] = $result['errorcode'];
				$ret['url'] = $result['code_img_url'];
				$this->insertRow($this->prename .'member_recharge', $para);

				//order记录
				$orderParm['order_number'] = $para['rechargeId'];
				$orderParm['username'] = mysql_escape_string($this->user['username']);
				$orderParm['recharge_amount'] = mysql_escape_string($para['amount']);
				$orderParm['state'] = 0;
				$orderParm['time'] = date("Y-m-d H:i:s",time()+28800-date("Z",time()));
				$this->insertRow($this->prename .'order', $orderParm);
			}else{
				$ret['errorcode'] = -1;
				$ret['errormsg'] = isset($result) ? $result['errormsg'] : '充值订单生产请求出错';
			}
		}
		//清空验证码session
	    unset($_SESSION[$this->vcodeSessionName]);
	    return $ret;
	}
	
	//生成签名
	public final function sign($order){
		ksort($order);
		$buff = "";
		foreach ($order as $k => $v)
		{
			if($k != "sign" && $v != "" && !is_array($v)){
				$buff .= $k . "=" . $v . "&";
			}
		}
		$buff.='key='.$this->KEY;
		$string = strtolower(md5($buff));
		return $string;
	}
	
	public final function warpParam($para){
		$subject = $this->BODYNOTE;
		$totalFee = $para["amount"];
		$body = $this->BODYNOTE;
		$payType = 23 == $_POST["mBankId"] ? 'pay_alipay_scan' : 'pay_weixin_scan';
		
		$order = array();
		if (isset($subject)) {
			$order["subject"] = trim($subject);
		}
		if (isset($totalFee)) {
			$order["total_fee"] = trim($totalFee*100);
		}
		if (isset($body)) {
			$order["body"] = trim($body);
		}
		if (isset($payType)) {
			$order["pay_type"] = trim($payType);
		}
		$order["mchNo"] = $this->MCHNO;
		$order["tag"] = $this->TAG;
		$order["version"] = $this->VERSION;
		$order["appid"] = $this->APPID;
		$order["mchorderid"] = $para['rechargeId'];
		$order["callback_url"] = $this->callbackUrl;
		return $order;
	} 
	
	public final function postCurl($postfield, $hearderinfo=array(), $ua="MyLinuxCURL"){
		if(!$postfield){
			return false;
		}
		$ch  = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->API);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30000);
		curl_setopt($ch, CURLOPT_TIMEOUT,30000);
		curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		if($hearderinfo){
			curl_setopt($ch,CURLOPT_HTTPHEADER,$hearderinfo);
		}
	
		$curl_return = curl_exec($ch);
		if(curl_errno($ch)){
			return false;    //curl出错
		}
		curl_close($ch);
		unset($ch);
		return $curl_return;
	}
}